/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ywb.edugui.view.user;

import com.ywb.edugui.controller.UserController;
import com.ywb.edugui.dao.UserDao;
import com.ywb.edugui.dao.facotry.DaoFactory;
import com.ywb.edugui.utils.VerifyCodeUtils;
import com.ywb.edugui.utils.BackgroundPanel;
import lombok.Data;

import javax.swing.*;
import java.awt.*;
import java.util.Locale;
import java.util.ResourceBundle;
/**
 *
 * @author 10147
 */
@Data
public class RegistrationView extends javax.swing.JFrame {
    // Variables declaration - do not modify
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbExit;
    private javax.swing.JButton jbRegistration;
    private javax.swing.JLabel jlCode;
    private javax.swing.JLabel jlCodeImage;
    private javax.swing.JLabel jlPassword;
    private javax.swing.JLabel jlRePassword;
    private javax.swing.JLabel jlUserName;
    private javax.swing.JPanel jpBackground;
    private javax.swing.JTextField tfCode;
    private javax.swing.JPasswordField tfPassword;
    private javax.swing.JPasswordField tfRePassword;
    private javax.swing.JTextField tfUsername;
    // End of variables declaration

    private UserController userController = new UserController(this);
    private UserDao userDao = DaoFactory.getUserDao();
    private String codeConfirm = "";
    /**
     * Creates new form LoginView
     */
    public RegistrationView() {

        initComponents();
        initCode();
        resetTextField();
        setLocationRelativeTo(null);
        tfUsername.grabFocus();
    }

    public RegistrationView(Locale locale) {
        this();
        setRegionText(locale);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
        ImageIcon image = new ImageIcon(LoginView.class.getResource("/images/background.jpg"));
        jpBackground = new BackgroundPanel(image.getImage());
        jPanel1 = new javax.swing.JPanel();
        jlUserName = new javax.swing.JLabel();
        jlPassword = new javax.swing.JLabel();
        tfUsername = new javax.swing.JTextField();
        jbRegistration = new javax.swing.JButton();
        jbExit = new javax.swing.JButton();
        jlRePassword = new javax.swing.JLabel();
        tfCode = new javax.swing.JTextField();
        jlCodeImage = new javax.swing.JLabel();
        jlCode = new javax.swing.JLabel();
        tfPassword = new javax.swing.JPasswordField();
        tfRePassword = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new Color(255,255,255,120));

        jlUserName.setText("username");

        jlPassword.setText("password");

        tfUsername.setToolTipText("E.g admin");
        tfUsername.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfUsernameActionPerformed(evt);
            }
        });

        jbRegistration.setText("registration");
        jbRegistration.setPreferredSize(new java.awt.Dimension(105, 28));
        jbRegistration.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbRegistrationActionPerformed(evt);
            }
        });

        jbExit.setText("exit");
        jbExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbExitActionPerformed(evt);
            }
        });

        jlRePassword.setText("re-password");

        tfCode.setToolTipText("admin");
        tfCode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfCodeActionPerformed(evt);
            }
        });

        jlCodeImage.setText("jLabel5");
        jlCodeImage.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jlCodeImageMouseReleased(evt);
            }
        });

        jlCode.setText("code");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addGap(28, 28, 28)
                                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                                .addComponent(jlPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addComponent(tfPassword))
                                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                                                .addComponent(jlUserName)
                                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                                .addComponent(tfUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                                .addComponent(jlRePassword, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addComponent(tfRePassword))
                                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                                                .addComponent(jlCode, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                                        .addComponent(tfCode, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(jlCodeImage, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addContainerGap()
                                                .addComponent(jbRegistration, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addGap(18, 18, 18)
                                                .addComponent(jbExit, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap(37, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(46, 46, 46)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(tfUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jlUserName))
                                .addGap(21, 21, 21)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jlPassword)
                                        .addComponent(tfPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jlRePassword)
                                        .addComponent(tfRePassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(tfCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jlCode))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jlCodeImage, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(36, 36, 36)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jbExit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jbRegistration, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(66, 66, 66))
        );

        javax.swing.GroupLayout jpBackgounrdLayout = new javax.swing.GroupLayout(jpBackground);
        jpBackground.setLayout(jpBackgounrdLayout);
        jpBackgounrdLayout.setHorizontalGroup(
                jpBackgounrdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpBackgounrdLayout.createSequentialGroup()
                                .addGap(0, 213, Short.MAX_VALUE)
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jpBackgounrdLayout.setVerticalGroup(
                jpBackgounrdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 367, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jpBackground, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jpBackground, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>

    private void tfUsernameActionPerformed(java.awt.event.ActionEvent evt) {
    }

    private void jbRegistrationActionPerformed(java.awt.event.ActionEvent evt) {
        String username = tfUsername.getText();
        String password= new String(tfPassword.getPassword());
        String rePassword = new String(tfRePassword.getPassword());
        String code = tfCode.getText();
        userController.Register(username, password, rePassword, code);
    }

    private void tfCodeActionPerformed(java.awt.event.ActionEvent evt) {

    }

    private void jbExitActionPerformed(java.awt.event.ActionEvent evt) {
        dispose();
        new LoginView().setVisible(true);
    }

    private void jlCodeImageMouseReleased(java.awt.event.MouseEvent evt) {
        initCode();
    }

    private void setRegionText(Locale locale) {
        ResourceBundle bundle = ResourceBundle.getBundle("i18n/resource", locale);
        jlCode.setText(bundle.getString("registration.code"));
        jbRegistration.setText(bundle.getString("registration.registration"));
        jbExit.setText(bundle.getString("registration.exit"));
        jlUserName.setText(bundle.getString("registration.username"));
        jlPassword.setText(bundle.getString("registration.password"));
        jlRePassword.setText(bundle.getString("registration.repassword"));
//        如果使用Paint绘制背景的话repaint这一步很重要,否则会影响整个效果
        jPanel1.repaint();
    }
    public void initCode(){
        //初始化验证码
        resetTextField(tfCode);
        jlCodeImage.setIcon(new ImageIcon(VerifyCodeUtils.getImage()));
        codeConfirm = VerifyCodeUtils.getText();
        jpBackground.repaint();
    }

    private void resetTextField(){
        Component[] jPanel = getRootPane().getContentPane().getComponents();
        Component[] components = ((JPanel)jPanel[0]).getComponents();
        for (Component component : components) {
            if(component instanceof JTextField) {
                ((JTextField) component).setText("");
            }
        }
    }
    public void resetTextField(JTextField... textFields){
        for(int i = 0; i < textFields.length; i++){
            textFields[i].setText("");
        }
    }



}
